"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = require("path");
const URL = tslib_1.__importStar(require("url"));
const utils_1 = require("../helper/utils");
const scripts_1 = tslib_1.__importDefault(require("./scripts"));
exports.default = async (options) => {
    const { REPO_NAME = '', TAG, REPO_ROOT = '' } = process.env;
    const fetchAndTag = `git fetch && git checkout tags/${TAG}`;
    // 项目的绝对路径
    const repoPath = path_1.join(REPO_ROOT, REPO_NAME);
    if (await utils_1.isExist(repoPath, true)) {
        if (await utils_1.isExist(path_1.join(repoPath, '.git/config'))) {
            try {
                utils_1.send('git fetching...');
                const { stdout, stderr } = await utils_1.exec(`git checkout . && ${fetchAndTag}`, {
                    cwd: repoPath,
                });
                utils_1.send(stderr);
                utils_1.send(stdout);
                await scripts_1.default(repoPath, options);
            }
            catch (err) {
                utils_1.send(err, false);
            }
        }
        else {
            utils_1.send(`${repoPath} 地址已存在，且该目录不是git项目，请手动删除该目录`, false);
        }
    }
    else if (await utils_1.isExist(REPO_ROOT, true)) {
        const { username, password, httpUrl, sshUrl } = options;
        let std;
        try {
            if (username && password && httpUrl) {
                utils_1.send('正在使用http(s)+username+password克隆代码中...');
                const u = URL.parse(httpUrl);
                const url = `${u.protocol}//${username}:${password}@${u.host}${u.path}`;
                std = await utils_1.exec(`git clone ${url}`, { cwd: REPO_ROOT });
            }
            else if (sshUrl) {
                utils_1.send('正在使用ssh克隆代码中...');
                const matched = sshUrl.match(/@([^:]+)/);
                if (matched) {
                    const domain = matched[1];
                    try {
                        if (!await utils_1.fileContain(domain)) {
                            await utils_1.exec(`ssh-keyscan ${domain} >> ~/.ssh/known_hosts`).catch(err => utils_1.send(err.message));
                        }
                        std = await utils_1.exec(`git clone ${sshUrl}`, { cwd: REPO_ROOT, timeout: 15 * 1000 });
                    }
                    catch (err) {
                        utils_1.send(err.message);
                        utils_1.send('正在尝试使用http(s)克隆代码');
                        std = await utils_1.exec(`git clone ${httpUrl}`, { cwd: REPO_ROOT });
                    }
                }
            }
            else if (httpUrl) {
                utils_1.send('正在使用http(s)克隆代码');
                std = await utils_1.exec(`git clone ${httpUrl}`, { cwd: REPO_ROOT });
            }
        }
        catch (err) {
            utils_1.send(err.message, false);
        }
        if (std) {
            utils_1.send(std.stdout);
            utils_1.send(std.stderr);
            const std1 = await utils_1.exec(fetchAndTag, { cwd: path_1.join(REPO_ROOT, REPO_NAME) });
            utils_1.send(std1.stdout);
            utils_1.send(std1.stderr);
            await scripts_1.default(repoPath, options);
        }
    }
    else {
        utils_1.send(`更新仓库失败，仓库所在根目录不存在`, false);
    }
};
